﻿using AvalSqlIndexManager.Models;
using DynamicData;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AvalSqlIndexManager.ViewModels
{
    public class NodeViewModel : ViewModelBase
    {
        /// <summary>
        /// 如果为null，即为table节点，不允许修改
        /// </summary>
        public TableIndex? TableIndex { get; set; }

       
        public NodeViewModel(string title,bool hasChild=false)
        {
            Title = title;
            if(hasChild)
                SubNodes = new ObservableCollection<NodeViewModel>();
        }

        public ObservableCollection<NodeViewModel>? SubNodes { get; }

        private string _title;
        public string Title
        {
            get
            {
                if (TableIndex != null && TableIndex.FieldsName != null)
                    return TableIndex.FieldsName;
                else
                    return _title;
            }
            set { this.RaiseAndSetIfChanged(ref _title, value); }
        }

        public void AddNode(TableIndex tableIndex)
        {
            if (SubNodes == null)
                return;
            if (string.IsNullOrEmpty(tableIndex.FieldsName))
                return;
            NodeViewModel subNode = new NodeViewModel(tableIndex.FieldsName);
            subNode.TableIndex = tableIndex;
            SubNodes.Add(subNode);
        }
    }
}
